package indi.woniu.service.impl;

import indi.woniu.entity.User;
import indi.woniu.entity.UserDo;
import indi.woniu.field.Tables;
import indi.woniu.mapper.UserDao;
import indi.woniu.mapper.UserMapper;
import indi.woniu.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Author: GaN601
 * @Date: 2021/12/21 15:24
 * @Description:
 */
@Service
public class UserServiceImpl implements UserService {
    private UserMapper userMapper;
    
    @Autowired
    public void setUserMapper(final UserMapper userMapper) {
        this.userMapper = userMapper;
    }
    
    
    /**
     * 查询用户
     *
     * @param username 用户名
     * @return 用户
     */
    @Override
    public UserDo selectByName(final String username) {
        
        return userMapper.selectUserByUsername(username);
    }
    
    @Override
    public List<UserDo> selectAllUser() {
        List<UserDo> userDos = userMapper.selectAllUser();
        userDos.forEach(userDo -> {
            userDo.setPassword(null);
        });
        return userDos;
    }
    
    /**
     * 根据用户名搜索用户角色
     *
     * @param username 用户名
     * @return 结果集
     */
    @Override
    public List<String> selectUserRole(String username) {
        return userMapper.selectUserRole(username);
    }
    
    /**
     * 获取用户可访问路径
     *
     * @param username 用户名
     * @return 返回该用户可访问的路径
     */
    @Override
    public List<String> selectUserCanToPath(final String username) {
        return userMapper.selectUserCanToPath(username);
    }
    
    @Autowired(required = false)
    private UserDao userDao;
    
    @Override
    public List<User> findByRoleId(int id) {
        List<User> list = userDao.findByRoleId(id);
        return list;
    }
    
    @Override
    public int deleteById(int id) {
        int i = userDao.deleteById(id);
        return i;
    }
    
    @Override
    public int updateById(int id) {
        return 0;
    }
    
    @Override
    public List<User> findAll() {
        return userDao.findAll();
    }
    
    @Override
    public List<String> selectByGroup(Tables table, final String field) {
        if (field.contains("password")) {
            return new ArrayList<>();
        }
        return userMapper.selectByGroup(table, field);
    }
    
    @Override
    public Integer update(final Integer id, final Map<String, Object> fieldsValues) {
        return userMapper.update(Tables.USER, fieldsValues, id);
    }
    
    @Override
    public Integer delete(final Integer id) {
        return userMapper.delete(Tables.USER, id);
    }
}
